package mce.cuce;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ShortenServlet
 */
@WebServlet("/ShortenServlet")
public class ShortenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShortenServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter pw = response.getWriter();
		pw.println("Shorten Page");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url = request.getParameter("url");
		if(url != null) {	// && UrlUtilities.isValid(url) shorten the url and view
			String shorty = UrlUtilities.shortenUrl(url);
			String longUrl = DbService.getLongUrl(shorty);
			
			if(longUrl != null) {
				if( longUrl.equals(url)) {	//already exists?
					showResult(request, response, shorty, longUrl);
				} else {	//save it
					if(DbService.addUrl(shorty, longUrl)) {	//insert successfull
						showResult(request, response, shorty, longUrl);
					} else {	//insert fail, error
						request.setAttribute("hataMesaji", "Db insert error");
						RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
						dispatcher.forward(request, response);
					}
				}
			} else {
				if(DbService.addUrl(shorty, longUrl)) {	//insert successfull
					showResult(request, response, shorty, longUrl);
				} else {	//insert fail, error
					request.setAttribute("hataMesaji", "Db insert error");
					RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
					dispatcher.forward(request, response);
				}
			}
		} else {	//No url or inValid url return to form page, show error.
			request.setAttribute("hataMesaji", "Invalid URL");
			RequestDispatcher dispatcher = request.getRequestDispatcher("error.jsp");
			dispatcher.forward(request, response);
		}
	}
	
	private void showResult(HttpServletRequest request, HttpServletResponse response, String s, String l) {
		request.setAttribute("shortUrl", s);
		request.setAttribute("longUrl", l);
		RequestDispatcher dispatcher = request.getRequestDispatcher("result.jsp");
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
